﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;

namespace Wicresoft.SMS.Data
{
    public class UserRoleService : IUserRoleService
    {
        IConnectStringProvider _csp;
        public UserRoleService(IConnectStringProvider provider)
        {
            _csp = provider;
        }

        //the return mustn't null
        public string[] GetRolesForUser(string username)
        {
            throw new NotImplementedException();
        }

        public bool ValidationUser(string key,string password)
        {
            if (key == null || password == null)
                return false;
            using (var db = new Entities(_csp.ConnectionString))
            {
                System.Data.Objects.ObjectParameter op = new System.Data.Objects.ObjectParameter("reutrnUserID",System.Data.DbType.Int32);
                db.cp_login(key, password, op);
                if (Convert.ToInt32(op.Value) > 0)
                    return true;
                return false;
            }
        }


        public long[] GetRolesValueForUser(string username)
        {
            List<long> roleslist = new List<long>();
            if (username == null)
                return roleslist.ToArray();
            using (var db = new Entities(_csp.ConnectionString))
            {
                var user = db.User.FirstOrDefault(p=>p.CIL == username);
                if (user != null && user.Role != null){
                    foreach (var role in user.Role){
                        roleslist.Add(role.RoleValue);
                    }
                }
            }
            return roleslist.ToArray();
        }

        public void LogIn(string username, bool remember)
        {
            //since username might be id alias and email
            FormsAuthentication.SetAuthCookie(username, remember);
        }

        public string GetRedirectUrl(string UserName, bool remember)
        {
            //!! if querystring and form are both not contain ReturnUrl , this may probably happen using ajax post
            // the FormsAuthentication.GetRedirectUrl will return FormsAuthentication.DefaultUrl
            string url = FormsAuthentication.GetRedirectUrl(UserName, remember);
            if (url == FormsAuthentication.DefaultUrl)
                return null;
            return url;
        }



        public User GetUserInfo(string key)
        {
            if (string.IsNullOrEmpty(key))
                return null;
            User user;
            using (var db = new Entities(_csp.ConnectionString))
            {
                int keyid;
                if (int.TryParse(key, out keyid)){
                    user = db.User.FirstOrDefault(p => (p.IsActive == true && (p.CIL == key || p.Email == key || p.UserID == keyid)));
                }
                else{
                    user = db.User.FirstOrDefault(p => (p.IsActive == true && (p.CIL == key || p.Email == key)));
                }
                if (user != null)
                {
                    user.Role.Load();
                    var temp2 = user.Department;
                }
            }
            return user;
        }


        public void LogOut()
        {
            FormsAuthentication.SignOut();
        }
    }
}
